package logica;

import persistencia.FabricaPersistenciaImm;
import persistencia.IPersistenciaVentaImm;
import datatypes.*;


public class LogicaVentaImm implements ILogicaVentaImm{
	
	private static LogicaVentaImm instance =null; //variable para crear la instancia de la logica venta imm
	
	private LogicaVentaImm(){
		
	}
	
	public static LogicaVentaImm getInstance(){
		
		if(instance == null){ //verifica si existe una instancia de la logica, sino devuelve la creada anteriormente
			instance = new LogicaVentaImm();			
		}
		return instance;
	}
	
	

	@Override
	public DataTicketVendido ventaTicket(DataTicket unDataTicket) {
		DataTicketVendido ticketVenta = new DataTicketVendido(0,0,1,"Error en SrvImm.Logica.LogicaVentaImm ventaTicket");
		try{
		IPersistenciaVentaImm i = FabricaPersistenciaImm.getPersistenciaVentaImm();
		boolean agencia = i.validarAgencia(unDataTicket.getId_agencia());
		if (agencia == true){
			float ultimoPrecio = i.consultarPrecioTicket();
			float importeTicket = calcularImporte(ultimoPrecio, unDataTicket);
			ticketVenta = i.grabarTicket(importeTicket, unDataTicket);
			return ticketVenta;
		}
		} catch (Exception e){
			System.out.print("Error en SrvImm.Logica.LogicaVentaImm ventaTicket");
			e.printStackTrace();
		}
		return ticketVenta;
	}

	@Override
	public float calcularImporte(float unPrecio, DataTicket unDataTicket) {
		float importe=9999;
		try{			
			int minutos = unDataTicket.getCant_min();
			float modulo	= minutos%30;
			float divi		= minutos/30;
			int cant =(int)divi;
			if (modulo > 0){
				cant = cant+1;
			}
			importe = unPrecio * cant;			
			return importe;
		}catch (Exception e){
			System.out.print("Error en SrvImm.Logica.LogicaVentaImm ventaTicket");
			e.printStackTrace();
		}
		return importe;
	}

}
